from django.db import models


# 影片时长表格，因为每个视频下单的价格受到时长的影响，所以单独提出来
class VideoTime(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('名称，比如1分钟、15分钟', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField('状态0为有效', max_length=11)
    factor = models.FloatField('价格因子', null=False, default=0.0)
    show_order = models.IntegerField(max_length=11)

    def to_dict(self, photo_style=''):
        return dict(
            id=self.id,
            name=self.name,
            factor=self.factor,
        )


# banner
class Advertisement(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('banner name', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    category_id = models.IntegerField('1首页2视频馆', max_length=11)
    image_file = models.CharField('图片路径', max_length=255, unique=False, blank=True)
    url = models.CharField('跳转地址可配', max_length=255, unique=False, blank=True)
    state = models.IntegerField('状态0为有效', max_length=11)
    site = models.IntegerField('暂时没用', max_length=11)
    type = models.IntegerField('0小程序内连接，1H5', max_length=11)
    show_order = models.IntegerField(max_length=11)

    def to_dict(self, photo_style=''):
        d = dict(
            id=self.id,
            name=self.name,
            image_file=self.image_file,
            url=self.url,
            type=self.type,
        )

        if not d['image_file']:
            d['image_file'] = 'https://media.joyfulmedia.cn/010.jpg'
        if photo_style:
            d['image_file'] += photo_style
        return d


# tag
class Tag(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('标签名称', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)

    def to_dict(self, photo_style=''):
        return dict(
            id=self.id,
            name=self.name,
        )


# 价格子选项
class PriceTypeDetail(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('选项名称', max_length=256, unique=False, blank=True)
    desc = models.CharField('描述', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)
    price = models.IntegerField('原价', max_length=11)
    transaction_price = models.IntegerField('优惠价格', max_length=11)

    def to_dict(self):
        return dict(
            id=self.id,
            name=self.name,
            desc=self.desc,
            price=self.price,
            transaction_price=self.transaction_price
        )


# 价格类型
class PriceType(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('名称', max_length=256, unique=False, blank=True)
    desc = models.CharField('描述', max_length=256, unique=False, blank=True)
    icon = models.CharField('图标', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField(max_length=11)
    type = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)
    price_type_detail = models.ManyToManyField(PriceTypeDetail, verbose_name='关联的子选项', null=True, default='', blank='')

    def to_dict(self):
        d = dict(
            id=self.id,
            name=self.name,
            desc=self.desc,
            type=self.type,
            icon=self.icon,
        )

        if self.price_type_detail:
            d['detail'] = [f.to_dict() for f in self.price_type_detail.all()]
        return d


# 视频类型，宣传片、广告片、微电影、三维动画
class VideoType(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('视频类型', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)
    price_type = models.ManyToManyField(PriceType, verbose_name='不同的视频报价因素不同', null=True, default='', blank='')

    def to_dict(self):
        return dict(
            id=self.id,
            name=self.name,
        )


class VideoOrderType(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    city = models.CharField('城市', max_length=256, unique=False, blank=True)
    second_city = models.CharField('城市', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)
    price_type_detail = models.ManyToManyField(PriceTypeDetail, verbose_name='价格子选项', null=True, default='', blank='')
    video_time = models.ForeignKey(VideoTime, verbose_name='视频类型', null=True, default=-1, blank=-1, on_delete=models.SET_DEFAULT)

    def to_dict(self):
        return dict(
            id=self.id,
            name=self.name,
            city=self.city,

        )


class AppUser(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    openid = models.CharField('openid', max_length=64, unique=True, blank=False)
    nickName = models.CharField('昵称', max_length=256, unique=False, blank=True)
    gender = models.IntegerField(max_length=11)
    language = models.CharField('语言', max_length=256, unique=False, blank=True)
    city = models.CharField('城市', max_length=256, unique=False, blank=True)
    province = models.CharField('省份', max_length=256, unique=False, blank=True)
    country = models.CharField('国家', max_length=256, unique=False, blank=True)
    avatarUrl = models.CharField('头像', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)

    def to_dict(self):
        return dict(
            id=self.id,
            nickName=self.nickName,
            openid=self.openid,
            gender=self.gender,
            city=self.city,
            province=self.province,
            country=self.country,
            avatarUrl=self.avatarUrl,

        )


class VideoOrderState(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('名称', max_length=256, unique=False, blank=True)


class VideoOrder(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    city = models.CharField('城市', max_length=256, unique=False, blank=True)
    second_city = models.CharField('城市', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.ForeignKey(VideoOrderState, verbose_name='订单状态', null=True, default=-1, blank=-1, on_delete=models.DO_NOTHING)
    video_type = models.ForeignKey(VideoType, verbose_name='视频类型', null=True, default=-1, blank=-1, on_delete=models.DO_NOTHING)
    price_type_detail = models.ManyToManyField(PriceTypeDetail, verbose_name='价格子选项', null=True, default='', blank='')
    video_time = models.ForeignKey(VideoTime, verbose_name='视频类型', null=True, default=-1, blank=-1, on_delete=models.DO_NOTHING)
    requirement = models.CharField('需求', max_length=256, unique=False, blank=True)
    contacts = models.CharField('城市', max_length=256, unique=False, blank=True)
    phone = models.CharField('电话', max_length=256, unique=False, blank=True)
    openid = models.CharField('openid', max_length=64, unique=False, blank=True)
    weixin_order_id = models.CharField('微信订单id', max_length=64, unique=False, blank=True)
    order_id = models.CharField('商户订单id', max_length=64, unique=False, blank=True)
    price = models.IntegerField('待支付价格', max_length=11)
    pay_amount = models.IntegerField('已支付金额', max_length=11)
    back_amount = models.IntegerField('已退款价格', max_length=11)

    def get_id(self):
        return dict(
            id=self.id,
        )

    def to_dict(self, type=1):
        d = dict(
            id=self.id,
            city=self.city,
            second_city=self.second_city,
            create_time=self.create_time,
            # url=self.url,
            price=self.price,
            pay_amount=self.pay_amount,
            back_amount=self.back_amount,
            state_code=self.state.id,
            state_name=self.state.name,
            requirement=self.requirement,
            contacts=self.contacts,
            phone=self.phone,
        )
        if type == 1:
            d['video_type'] = self.video_type.to_dict()
            d['video_time'] = self.video_time.to_dict()
            d['total_price'] = 0
            d['transaction_price'] = 0
            d['price_detail'] = []
            for detail in self.price_type_detail.all():
                d['total_price'] += detail.price
                d['transaction_price'] += detail.transaction_price
                d['price_detail'].append({
                    'price_item': detail.to_dict()
                })
            d['total_price'] = d['total_price'] * self.video_time.factor
            d['transaction_price'] = d['transaction_price'] * self.video_time.factor
        return d


class Video(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('标题', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    image_file = models.CharField('封面', max_length=255, unique=False, blank=True)
    url = models.CharField('链接地址', max_length=255, unique=False, blank=True)
    state = models.IntegerField(max_length=11)
    site = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)
    tags = models.ManyToManyField(Tag, verbose_name='包含视频', null=True, default='', blank='')
    play_time = models.IntegerField('播放次数', max_length=11, null=True, default=0)
    video_order_type = models.ForeignKey(VideoOrderType, verbose_name='视频类型', null=True, default=-1, blank=-1, on_delete=models.SET_DEFAULT)
    desc = models.CharField('描述', max_length=512, null=True, default='')
    video_type = models.ForeignKey(VideoType, verbose_name='UP主', null=True, default=-1, blank=-1, on_delete=models.SET_DEFAULT)

    def to_dict(self, photo_style='', type=1):
        d = dict(
            id=self.id,
            name=self.name,
            image_file=self.image_file,
            # url=self.url,
            create_time=self.create_time,
            play_time=self.play_time,
            desc=self.desc,
        )
        if not d['image_file']:
            d['image_file'] = 'https://media.joyfulmedia.cn/010.jpg'
        if photo_style:
            d['image_file'] += photo_style
        if self.video_type:
            d['video_type'] = self.video_type.to_dict()
        if self.tags:
            d['tags'] = [f.to_dict() for f in self.tags.all()]
        if type == 1:
            price = 0
            d['transaction_price'] = 0
            for detail in self.video_order_type.price_type_detail.all():
                price += detail.price
                d['transaction_price'] += detail.transaction_price
            d['price'] = price * self.video_order_type.video_time.factor
            d['transaction_price'] = d['transaction_price'] * self.video_order_type.video_time.factor
        if type == 2:
            d['video_time'] = self.video_order_type.video_time.to_dict()
            d['price_detail'] = []
            d['city'] = self.video_order_type.city
            d['total_price'] = 0
            d['transaction_price'] = 0
            for detail in self.video_order_type.price_type_detail.all():
                d['total_price'] += detail.price
                d['transaction_price'] += detail.transaction_price
                d['price_detail'].append({
                    'price_factor': detail.pricetype_set.first().to_dict(),
                    'price_item': detail.to_dict()
                })
            d['total_price'] = d['total_price'] * self.video_order_type.video_time.factor
            d['transaction_price'] = d['transaction_price'] * self.video_order_type.video_time.factor

        return d


class GoodsBanner(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('类别', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField('状态', max_length=11)
    site = models.IntegerField(max_length=11)
    show_order = models.IntegerField('默认排序', max_length=11)
    url = models.CharField('链接地址', max_length=255, unique=False, blank=True)

    def to_dict(self):
        d = {
            'url': self.url,
            'name': self.name,
        }
        if self.url:
            d['url'] += '/750'
        return d


class GoodsPrice(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('类别', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField('状态', max_length=11)
    site = models.IntegerField(max_length=11)
    show_order = models.IntegerField('默认排序', max_length=11)
    price = models.IntegerField('原始价格', max_length=11)
    transaction_price = models.IntegerField('折后价格', max_length=11)
    unit = models.CharField('单位', max_length=256, unique=False, blank=True)
    inventory = models.IntegerField('库存', max_length=11)

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'price': self.price,
            'transaction_price': self.transaction_price,
            'unit': self.unit,
            'inventory': self.inventory,
        }


class Goods(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('标题', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    image_file = models.CharField('封面', max_length=255, unique=False, blank=True)
    tags = models.CharField('标签', max_length=255, unique=False, blank=True)
    state = models.IntegerField(max_length=11)
    site = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)
    sales = models.IntegerField(max_length=11)
    price = models.ManyToManyField(GoodsPrice, verbose_name='分类', null=True, default='', blank='')
    banner = models.ManyToManyField(GoodsBanner, verbose_name='分类', null=True, default='', blank='')
    detail = models.CharField('链接地址', max_length=255, unique=False, blank=True)
    content = models.CharField('链接地址', max_length=512, unique=False, blank=True)
    city = models.CharField('城市', max_length=512, unique=False, blank=True)

    def to_dict(self, photo_style='', type=1):
        d = {
            'id': self.id,
            'name': self.name,
            'image_file': self.image_file,
            'tags': self.tags,
            'sales': self.sales,
            'detail': self.detail,
            'city': self.city,
            'content': self.content,

        }
        if photo_style:
            d['image_file'] += photo_style
        if d['detail']:
            d['detail'] += '/750'

        if self.price and type == 1:
            d['price'] = self.price.order_by('show_order').first().to_dict()
        if self.price and type == 2:
            d['price'] = [key.to_dict() for key in self.price.all()]
        if self.banner:
            d['banner'] = [key.to_dict() for key in self.banner.all()]
        return d


# 商品评论
class GoodsComment(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField(max_length=11)
    site = models.IntegerField(max_length=11)
    goods_id = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)
    content = models.CharField('链接地址', max_length=512, unique=False, blank=True)
    price = models.ForeignKey(GoodsPrice, verbose_name='分类', null=True, default=-1, blank=-1, on_delete=models.SET_DEFAULT)
    user = models.ForeignKey(AppUser, verbose_name='评论者', null=True, default=-1, blank=-1, on_delete=models.SET_DEFAULT)

    def to_dict(self, photo_style=''):
        d = {
            'id': self.id,
            'create_time': self.create_time,
            'content': self.content,

        }
        if self.price:
            d['type'] = self.price.to_dict()

        if self.user:
            d['user'] = self.user.to_dict()

        return d


class VideoCity(models.Model):
    id = models.AutoField('主键id', max_length=11, primary_key=True)
    name = models.CharField('标题', max_length=256, unique=False, blank=True)
    create_time = models.DateTimeField('创建时间', null=False, auto_now_add=True)
    update_time = models.DateTimeField('修改时间', null=False, auto_now=True)
    state = models.IntegerField(max_length=11)
    site = models.IntegerField(max_length=11)
    show_order = models.IntegerField(max_length=11)

    def to_dict(self):
        d = {
            'id': self.id,
            'name': self.name,
        }
        return d
